//
//  CustomTF.swift
//  CMDB
//
//  Created by 曹桂祥 on 17/4/6.
//  Copyright © 2017年 CMDB. All rights reserved.
//

import UIKit

class CustomTF: CMDBBaseView {
    
    let leftMargin      = (CMDB_Screen_Width - 630 * CMDB_Rate) * 0.5
    
    var text : String?  = ""
    
    var imageIconView   = UIImageView.getInstance()
    
    var seprateView     = UIView.getInstance()
    
    var leftLabel       = UILabel.getInstance()
    
    var tf:UITextField? = UITextField()
    
    var tid : String?
    
    var maxLength : Int = 11

    
    // the param data
    override var data: AnyObject?{
        
        willSet {
            
            if let _ = newValue {
                
                tf?.text = newValue as? String
                
                text     = newValue as? String
                
                tf?.allowsEditingTextAttributes
                
            }
        }
    }
    
    // clearButton Model
    var clearButtonMode : UITextFieldViewMode?{
        
        willSet{
            if let _ = newValue {
                tf?.clearButtonMode = newValue!
            }
        }
        
    }
    
    // textField textColor
    var textColor : UIColor? {
        
        willSet{
            if let _ = newValue {
                tf?.textColor = newValue!
            }
        }
    }
    
    //the type of keyBoardType
    var keyBoardType : UIKeyboardType? {
        
        willSet{
            if let _ = newValue {
                tf?.keyboardType = newValue!
            }
        }
        
    }
    
    //is editing
     var editing: Bool? {
        
        willSet {
            if let _ = newValue {
                
                tf?.enabled = newValue!
            }
        }
    }

    //the return type
    var returnKeyType: UIReturnKeyType? {
        
        willSet (newValue) {
            if let _ = newValue {
                tf?.returnKeyType = newValue!
            }
        }
    }
    
    //is Security
    var secureTextEntry: Bool? {
        willSet {
            if let _ = newValue {
                tf?.secureTextEntry = newValue!
            }
        }
    }
    
    var iconImageName : String? {
        willSet{
            
            if let _ = newValue {
                
                imageIconView.image = UIImage(named: newValue!)
                
                imageIconView.sizeToFit()
                
                imageIconView.snp_makeConstraints {[weak self] (make) in
                    make.left.equalTo(0)
                    make.width.equalTo(self!.imageIconView.width)
                    make.centerY.equalTo(5)
                    
                }
                
            }
        }
    }
    
    var leftFont : UIFont? {
        
        willSet{
            
            if let _ = newValue {
                
                leftLabel.font = newValue!
                
                leftLabel.sizeToFit()
                
                leftLabel.snp_makeConstraints { [weak self] (make) in
                    make.left.equalTo(self!.imageIconView.snp_right).offset(10)
                    make.width.equalTo(self!.leftLabel.width)
                    make.centerY.equalTo(self!.imageIconView.snp_centerY)
                }
                
                tf?.font = newValue
                
                tf!.snp_makeConstraints {[weak self] (make) in
                    make.left.equalTo(self!.leftLabel.snp_right).offset(12)
                    make.right.equalTo(0)
                    make.centerY.equalTo(self!.imageIconView.snp_centerY)
                }
            }
        }
    }

    var attributedPlaceHolder: NSAttributedString? {
        
        willSet (newValue) {
            if let _ = newValue {
                tf?.attributedPlaceholder = newValue
            }
        }
    }
    
    var placeHolder: String? {
        
        willSet (newValue) {
            if let _ = newValue {
                tf?.placeholder = newValue
            }
        }
    }

}


extension CustomTF{
    
    override func addOwnSubviews() {
        
        super.addOwnSubviews()
        
        //imageIcon
        addSubview(imageIconView)
        
        //textField
        tf?.delegate      = self
        
        tf?.keyboardType  = .Default
        
        tf?.returnKeyType = .Next
        
        tf?.addTarget(self, action: #selector(editChange(_:)), forControlEvents: .EditingChanged)
        
        addSubview(tf!)
        
        //separateView
        seprateView.backgroundColor = UIColor.Hex6(0x00000, alpha: separateLineAlpha)
        
        addSubview(seprateView)
        
        //leftLabel 
        addSubview(leftLabel)

    }
    
    override func buildSubViews() {
        
        super.buildSubViews()
        
        seprateView.snp_makeConstraints { (make) in
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.bottom.equalTo(0)
            make.height.equalTo(1)
        }
    }
    
    func clearText(){
        
        text = ""
        
        tf?.text = ""
        
    }
}

extension CustomTF : UITextFieldDelegate{
    
    func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
        
        return range.length == 1 && (string as NSString).length == 0 ? true : (textField.text! as NSString).length < maxLength
        
    }
    
    func textFieldShouldReturn(textField: UITextField) -> Bool {
        
        print(tid)
         mDelegate?.onItemClickType!("ReturnKey", data: tid, indexPath: nil)
        return true
    }
    
    @objc func editChange(tf:UITextField){
        
        text = tf.text
        
        if  clearButtonMode != nil{
            mDelegate?.onItemClickType!("BeginEdit", data: tf.text, indexPath: nil)
        }
    }
}

// 释放响应者
extension CustomTF {
    
    override func becomeFirstResponder() -> Bool {
        tf?.becomeFirstResponder()
        return true
    }
    override func resignFirstResponder() -> Bool {
        tf?.resignFirstResponder()
        return true
    }
}

